package listener

import (
	"encoding/json"
	"fmt"
	"hcy-api/structs/idp"
	"hcy-api/structs/tables"
)

var InstructionUtils = new(instructionUtils)

type instructionUtils struct {
}

func (u instructionUtils) GetCatchKey(inst idp.PluginInstruct) string {
	return fmt.Sprintf("%s_%v", inst.Cmd, inst.Args)
}

func (u instructionUtils) TaskForFileAndProcess(m tables.IdpTask) (inst idp.PluginInstruct, err error) {
	switch m.TaskType {
	case tables.IdpTaskTypeForCheckFile:
		content := new(tables.IdpTaskDetailsForFile)
		err = json.Unmarshal([]byte(m.Details), content)
		if err != nil {
			return
		} else {
			inst = idp.PluginInstruct{
				Cmd: "file.exe",
				Args: []string{fmt.Sprintf("-taskId=%d", m.Id),
					fmt.Sprintf("-path=%s", content.FilePath)},
			}
		}
	case tables.IdpTaskTypeForCheckProcess:
		content := new(tables.IdpTaskDetailsForProcess)
		err = json.Unmarshal([]byte(m.Details), content)
		if err != nil {
			return
		} else {
			inst = idp.PluginInstruct{
				Cmd: "process.exe",
				Args: []string{fmt.Sprintf("-taskId=%d", m.Id),
					fmt.Sprintf("-name=%s", content.ProcessName),
					fmt.Sprintf("-exePath=%s", content.ExePath),
				},
			}
		}
	}
	return
}
